import wx from '../api/jweixin-1.6.0.js'
import {getWechatShareConfig} from '@/api/public.js'

export default {
	data() {
		return {
			weixinFlag: false,
			extinfo: '',
			appid: '',
			isIos: false,
			ua: undefined,
		}
	},
	onLoad() {
		console.log('混入周期')
		this.getSystemInfo()
	},
	mounted() {
		if(!this.weixinFlag&&this.isIos){
			var btn = document.getElementById('launch-btn');
			btn.addEventListener('launch', function(e) {
				console.log('success');
			});
			btn.addEventListener('error', function(e) {
				// alert('fail',);
				// alert(JSON.stringify(e.detail))
				uni.showModal({
					title: '系统消息',
					content: '您的手机未找到当前应用，是否去下载？',
					success: function(res) {
						if (res.confirm) {
							uni.reLaunch({
								url: '/pages/webMobile/webMobile'
							})
							console.log('用户点击确定');
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			});
		}
	},
	methods: {
		getSystemInfo() {
			uni.getSystemInfo({
				success: (res) => {
					this.ua = res.ua
					// alert(this.ua)
					this.osName = res.osName
					if (this.ua) {
						let ua = this.ua.toLowerCase();
						if (ua == 'app') { //APP内
							this.weixinFlag = false
							this.isIos = false
						} else { //APP外
							// 安卓微信端
							if (ua.indexOf('micromessenger') > -1 && this.osName !== 'ios') {
								this.weixinFlag = true
								this.JsApiTicketApi();
							}else{
								this.weixinFlag = false;
								this.isIos = true
							}
						}
					}
				}
			})
		},
		urlencode(str) {
			str = (str + '').toString();
			return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
			replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
		},
		watchVisibility() {
			window.addEventListener('visibilitychange', () => {
				// 监听页面visibility
				if (document.hidden) {
					// 如果页面隐藏了，则表示唤起成功，这时候需要清除下载定时器
					clearTimeout(this.timer)
				}
			})
		},
		async JsApiTicketApi() {
			let _this = this;
			let url = this.urlencode(window.location.href)
			// let arr = url.split('?')
			// urls = arr[0]
			// let url = this.baseViewUrl
			getWechatShareConfig(url).then((res)=>{
				console.log(res);
				if (res.code == 200) {
					_this.appid = res.data.appId
					wx.config({
						appId: res.data.appId, // 必填，公众号的唯一标识
						timestamp: res.data.timestamp, // 必填，生成签名的时间戳
						nonceStr: res.data.nonceStr, // 必填，生成签名的随机串
						signature: res.data.signature, // 必填，签名
						jsApiList: ['onMenuShareTimeline'], // 必填，需要使用的JS接口列表
						openTagList: ['wx-open-launch-app']
					});
					wx.ready((res1) => {
						// console.log('成功--------' + res1);
					});
					wx.error((res2) => {
						// alert(JSON.stringify(res2));
					});
				}
			})
			// uni.request({
			// 	url: `${_this.baseUrl}app/appsystem/common/getWechatShareConfig?url=${url}`,
			// 	success: (res) => {
			// 		// console.log(res)
					
			// 	}
			// })
		},
	}
}